<?php
namespace News\Controller;
use Think\Controller;
/*
	初始化
*/
class InitController extends Controller {
	
	protected $topmenu = array(); // 后台顶部菜单
	protected $sidebarmenu = array(); // 后台左侧菜单
	
	// 初始化
	protected  function _initialize() {
		
		// 登录验证
		if (!session('?logined') || session('logined') !== true || !session('?uid') || !session('uid')) {
			$this->redirect('Admin/Login/index');
		}
		
		// 定义菜单
		$this->topmenu = array(
			array('title'=>'仪表盘','url'=>U('Admin/Index/Index',array('k'=>0)),'on'=>0),
			array('title'=>'文章','url'=>U('Admin/ArticleSite/articleList',array('k'=>1,'sk'=>0)),'on'=>0),
			array('title'=>'会员','url'=>U('Admin/User/memberList',array('k'=>2)),'on'=>0),
			array('title'=>'页面','url'=>U('Admin/Single/singleList',array('k'=>3,'sk'=>0)),'on'=>0),
			array('title'=>'商城','url'=>U('Admin/Shopping/goodsList',array('k'=>4)),'on'=>0),
			array('title'=>'用户','url'=>U('Admin/Admin/userList',array('k'=>5,'sk'=>0)),'on'=>0),
			array('title'=>'设置','url'=>U('Admin/Option/siteOpt',array('k'=>6)),'on'=>0),
			array('title'=>'提现','url'=>U('Admin/Withdrawal/Withdrawal_rule',array('k'=>7)),'on'=>0),
			array('title'=>'音乐','url'=>U('Admin/Music/musicList',array('k'=>8)),'on'=>0),
			array('title'=>'图片素材','url'=>U('Admin/Image/lists',array('k'=>10)),'on'=>0),
			array('title'=>'微信','url'=>U('Admin/Wechats/Setting',array('k'=>9)),'on'=>0)

		);
		
		$this->sidebarmenu = array(
			array(
				array('title'=>'发布文章','url'=>U('Admin/Article/articleList',array('k'=>0,'sk'=>0)),'on'=>0),
				array('title'=>'文章列表','url'=>U('Admin/Article/articleList',array('k'=>0,'sk'=>1)),'on'=>0),
			),
			array(
				array('title'=>'文章列表','url'=>U('Admin/ArticleSite/articleList',array('k'=>1,'sk'=>0)),'on'=>0),
				array('title'=>'发布文章','url'=>U('Admin/ArticleSite/articleAdd',array('k'=>1,'sk'=>1)),'on'=>0),
				array('title'=>'文章分类','url'=>U('Admin/ArticleCate/articleCateList',array('k'=>1,'sk'=>2)),'on'=>0),
				array('title'=>'添加分类','url'=>U('Admin/ArticleCate/articleCateAdd',array('k'=>1,'sk'=>3)),'on'=>0),				
			),
			array(
				array('title'=>'会员列表','url'=>U('Admin/User/memberList',array('k'=>2,'sk'=>0)),'on'=>0),
				array('title'=>'会员文章','url'=>U('Admin/Article/articleList',array('k'=>2,'sk'=>1)),'on'=>0),
				array('title'=>'会员广告','url'=>U('Admin/User/adList',array('k'=>2,'sk'=>2)),'on'=>0),
				/* array('title'=>'会员审核','url'=>U('Admin/User/memberCheck',array('k'=>2,'sk'=>1)),'on'=>0 */
				array('title'=>'收款神器申请','url'=>U('Admin/User/pos_apply_list',array('k'=>2,'sk'=>2)),'on'=>0),
				array('title'=>'升级支付流水','url'=>U('Admin/User/user_paylog_list',array('k'=>2,'sk'=>3)),'on'=>0),
				array('title'=>'佣金收入','url'=>U('Admin/User/commissionList',array('k'=>2,'sk'=>4)),'on'=>0),
			),
			array(
				array('title'=>'所有页面','url'=>U('Admin/Single/singleList',array('k'=>3,'sk'=>0)),'on'=>0),
				array('title'=>'新建页面','url'=>U('Admin/Single/singleAdd',array('k'=>3,'sk'=>1)),'on'=>0),
				array('title'=>'页面栏目','url'=>U('Admin/SingleCate/singleCateList',array('k'=>3,'sk'=>2)),'on'=>0),
				array('title'=>'添加栏目','url'=>U('Admin/SingleCate/singleCateAdd',array('k'=>3,'sk'=>3)),'on'=>0),
			),
			array(
				array('title'=>'发布商品','url'=>U('Admin/Shopping/addGoods',array('k'=>4,'sk'=>0)),'on'=>0),
				array('title'=>'商品库','url'=>U('Admin/Shopping/goodsList',array('k'=>4,'sk'=>1)),'on'=>0),
				array('title'=>'商品规格','url'=>U('Admin/Shopping/attrList',array('k'=>4,'sk'=>2)),'on'=>0),
				array('title'=>'商品分类','url'=>U('Admin/Shopping/cateList',array('k'=>4,'sk'=>3)),'on'=>0),
			),
			array(
				array('title'=>'用户列表','url'=>U('Admin/Admin/userList',array('k'=>5,'sk'=>0)),'on'=>0),
				array('title'=>'添加用户','url'=>U('Admin/Admin/register',array('k'=>5,'sk'=>1)),'on'=>0),
			),
			array(
				array('title'=>'站点设置','url'=>U('Admin/Option/siteOpt',array('k'=>6,'sk'=>0)),'on'=>0),
				array('title'=>'导航列表','url'=>U('Admin/Navi/naviList',array('k'=>6,'sk'=>1)),'on'=>0),
				array('title'=>'添加导航','url'=>U('Admin/Navi/naviAdd',array('k'=>6,'sk'=>2)),'on'=>0),
				array('title'=>'会员等级设置','url'=>U('Admin/Option/levelList',array('k'=>6,'sk'=>3)),'on'=>0),
				array('title'=>'微信设置','url'=>U('Admin/Option/wx_pay',array('k'=>6,'sk'=>4)),'on'=>0),
				array('title'=>'支付宝设置','url'=>U('Admin/Option/ali_pay',array('k'=>6,'sk'=>5)),'on'=>0),
				array('title'=>'佣金设置','url'=>U('Admin/Commission/Index',array('k'=>6,'sk'=>6)),'on'=>0),
			),
			array(
				array('title'=>'提现规则','url'=>U('Admin/Withdrawal/Withdrawal_rule',array('k'=>7,'sk'=>0)),'on'=>0),
				array('title'=>'提现记录','url'=>U('Admin/Withdrawal/Withdrawal_list',array('k'=>7,'sk'=>1)),'on'=>0),
			//	array('title'=>'微信设置','url'=>U('Admin/Option/wx_pay',array('k'=>7,'sk'=>2)),'on'=>0),
			//	array('title'=>'支付宝设置','url'=>U('Admin/Option/ali_pay',array('k'=>7,'sk'=>3)),'on'=>0),

			),
			array(
				array('title'=>'音乐列表','url'=>U('Admin/Music/musicList',array('k'=>8,'sk'=>0)),'on'=>0),
				array('title'=>'上传音乐','url'=>U('Admin/Music/upMusic',array('k'=>8,'sk'=>1)),'on'=>0),
			),
			array(
				array('title'=>'微信参数','url'=>U('Admin/Wechats/Setting',array('k'=>9,'sk'=>0)),'on'=>0),
				array('title'=>'自定义菜单','url'=>U('Admin/Wechats/MenuDisplay',array('k'=>9,'sk'=>1)),'on'=>0),
				array('title'=>'图文回复列表','url'=>U('News/Rule/RuleDisplay',array('k'=>9,'sk'=>2)),'on'=>0),
				array('title'=>'图文回复添加','url'=>U('News/Rule/PostKeyword',array('k'=>9,'sk'=>3)),'on'=>0),
				array('title'=>'没有关键字回复','url'=>U('Admin/Rule/System',array('k'=>9,'sk'=>4,'op'=>'default')),'on'=>0),
				array('title'=>'关注回复','url'=>U('Admin/Rule/System',array('k'=>9,'sk'=>5,'op'=>'welcome')),'on'=>0),
				array('title'=>'消息通知','url'=>U('Admin/Wechats/Wxnotify',array('k'=>9,'sk'=>6)),'on'=>0),
			),
			array(
				array('title'=>'图片素材','url'=>U('Admin/Image/lists',array('k'=>10,'sk'=>0)),'on'=>0),
				array('title'=>'图片素材添加','url'=>U('Admin/Image/imgadd',array('k'=>10,'sk'=>1)),'on'=>0),
			)
			
		);
		
	}
	
	// 文件上传操作
	protected function upload($_dir='') {
		$upload = new \Think\Upload();
		$upload->maxSize  = C('upload_max_size') ;
		$upload->exts     = C('upload_allow_exts');
		if ($_dir) {
			$upload->rootPath = C('upload_path') . '/' . $_dir . '/';
		} else {
			$upload->rootPath = C('upload_path') . '/';
		}
		$upload->autoSub = C('upload_autosub');
		$upload->subName = C('upload_subname');
		
		// 上传文件
		$info   =   $upload->upload();
		if (!$info) {
			echo $upload->getError();exit();
		}
		
		if (isset($info['imgFile']) && !empty($info['imgFile'])) {
			$info['imgFile']['savepath'] = $upload->rootPath . $info['imgFile']['savepath'];
			// 查询数据库图片文件是否存在
			$attaModel = M('Attachment');
			$attaInfo = $attaModel->where(array('authcode'=>$info['imgFile']['md5']))->find();
			if ($attaInfo) {
				// 删除上传的文件
				$filename = $info['imgFile']['savepath'] . $info['imgFile']['savename'];
				@unlink($filename);
				$info['imgFile']['savepath'] = $attaInfo['filepath'];
				$info['imgFile']['savename'] = $attaInfo['filename'];
				$info['imgFile']['size'] = $attaInfo['filesize'];
				$info['imgFile']['ext'] = $attaInfo['fileext'];
			} else {
				//记录数据库
				$isimage = 0;
				$_filetype = $info['imgFile']['type'];
				if ($_filetype == 'image/jpeg' || $_filetype == 'image/jpg' || $_filetype == 'image/png' || $_filetype == 'image/gif') {
					$isimage = 1;
				}
				
				$data['filename'] = $info['imgFile']['savename'];
				$data['filepath'] = $info['imgFile']['savepath'];
				$data['filetype'] = $info['imgFile']['type'];
				$data['filesize'] = $info['imgFile']['size'];
				$data['fileext']  = $info['imgFile']['ext'];
				$data['isimage']  = $isimage;
				$data['isthumb']  = 0;
				$data['userid']   = session('uid');
				$data['uploadtime'] = NOW_TIME;
				$data['status'] = 1;
				$data['authcode'] =  $info['imgFile']['md5'];
				$attaModel->add($data);
			}
			
			return $info['imgFile'];
		}
		
		return $info;
	}
	
	// 分页配置返回分页类
	protected function setPageHander($count,$getRows=15){
		if (!$count || !is_numeric($count)) {
			return false;
		}
		$Page  = new \Think\Page($count,$getRows);
		// 分页配置设置
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','末页');
		
		return $Page;
	}
	
	// 空操作
	public function _empty(){
		$this->redirect('Admin/Index/index');
	}

}